home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / grocery.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  2.6 KB  |  67 lines

  1. // grocery.cpp
  2. //
  3. // Example program uses the btree dictionary template to make a
  4. // grocery list.  Names of grocery items and their corresponding
  5. // price are read in one at a time and added to the dictionary
  6. // until an 'x' (exit) is typed.  An iterator template is then
  7. // created and used to cycle through the dictionary printing
  8. // each grocery item and running a total tally.  When iteration
  9. // is finished, the total price of all the groceries is printed.
  10. //
  11. #include <cm/include/cmtbdict.h>
  12. #include <cm/include/cmstring.h>
  13.  
  14. // Create typedefs to make the code easier to read and understand.
  15. //
  16. typedef CmTBTreeDictionary<CmString, float> GroceryList;
  17. typedef CmTAssociation<CmString, float>     GroceryItem;
  18. typedef CmTBTreeIterator<GroceryItem>       GroceryIterator;
  19.  
  20. // Macros are used in place of the association's "value" and "object"
  21. // functions for readability.
  22. //
  23. #define NAME key
  24. #define PRICE object
  25.  
  26. void main()
  27. {
  28.   GroceryList list;                        // Declare dictionary.
  29.   CmString    stemp;                       // Temp for item name input.
  30.   float       ftemp;                       // Temp for item price input.
  31.   Bool        done = FALSE;                // Boolean data type.
  32.  
  33.   while (!done)                            // While boolean is true,
  34.   {
  35.     cout << "Enter grocery item (\"x\" when done): " << flush;
  36.     cin  >> stemp;                         // Read a new item name.
  37.  
  38.     if (stemp[0] == 'x')                   // If "x", then finished.
  39.       done = TRUE;
  40.  
  41.     else if (stemp.length())               // If not empty string,
  42.     {
  43.       cout << "Enter item price                  : " << flush;
  44.       cin  >> ftemp;                       // Read the item price.
  45.       cin.ignore();
  46.       list.addKeyAndObject(stemp, ftemp);  // Add name and price to dictionary.
  47.     }
  48.   }
  49.  
  50.   cout.setf(ios::fixed, ios::floatfield);  // Setup for float output.
  51.   cout.precision(2);
  52.   cout << endl << "Grocery List" << endl;  // Print list header.
  53.   cout << "----------------------------------------" << endl;
  54.  
  55.   float           total = 0;
  56.   GroceryIterator iterator(list);          // Create an iterator.
  57.   while (iterator)                         // While iterator is ok,
  58.   {
  59.     GroceryItem item = iterator++;         // Get next grocery item.
  60.     total += item.PRICE();                 // Add price to running total.
  61.     cout << item.NAME() << " $" << item.PRICE() << endl;  // Print name/price.
  62.   }
  63.  
  64.   cout << "----------------------------------------" << endl;
  65.   cout << "Total cost is $" << total << endl;  // Print total cost.
  66. }
  67.